<?php

/**
 * A utility package for creating pagination controls for a data set.
 * 
 * @author Jason Coward <jason@opengeek.com> (MODx)
 * Modified for FoxyBack by Jeremy Saye.
 */
	
/**
 * Generates the pagination.
 */
function createPagination($start=0, $many=50, $total=NULL, $limit_str) {

	// Create the links.
	$paging_links = createPagingNavigation($many, $total, $limit_str);

	$pagination = '<div id="pagination">';
	if ($paging_links) {
		$paging_links = '<ul>'.$paging_links.'</ul>';
	}
	$pagination .= '<span id="paginationLinks" class="clearfix">'.$paging_links.'</span>';
	$pagination .= '<span id="maxDisplayRecords" class="clearfix"><select style="display:inline" name="many" onchange="this.form.submit();">';
	$pageSizes= array (10, 25, 50, 100, 250);
	for ($i= 0; $i < count($pageSizes); $i ++) {
		$pagination .= '<option value="'.$pageSizes[$i].'"';
		$pagination .= $many == $pageSizes[$i] ? ' selected="selected" ' : '';
		$pagination .= '>'.$pageSizes[$i].'</option>';
	}
	$pagination .= '</select> per page</span>';
	$pagination .= '</div>';

	return $pagination;
}

/**
 * Generates optional paging navigation controls for the table.
 * 
 * @param $numRecords The number of records to show per page.
 * @param $total The total number of records to paginate on.
 * @param $qs An optional query string to be appended to the paging links
 */
function createPagingNavigation($numRecords, $total, $qs='') {
	$currentPage= (is_numeric($_GET['page']) ? $_GET['page'] : 1);
	$numPages= ceil($total / $numRecords);
	if ($numPages > 1) {
		$currentURL= empty($qs)? '': '?'.$qs;
		if ($currentPage > 6) {
			$nav .= createPageLink($currentURL, 1, 'First');
		}
		if ($currentPage != 1) {
			$nav .= createPageLink($currentURL, $currentPage -1, '&lt;&lt;');
		}
		$offset= -4 + ($currentPage < 5 ? (5 - $currentPage) : 0);
		$i= 1;
		while ($i < 10 && ($currentPage + $offset <= $numPages)) {
			if ($currentPage == $currentPage + $offset)
				$nav .= createPageLink($currentURL, $currentPage + $offset, $currentPage + $offset, true);
			else
				$nav .= createPageLink($currentURL, $currentPage + $offset, $currentPage + $offset);
			$i ++;
			$offset ++;
		}
		if ($currentPage < $numPages) {
			$nav .= createPageLink($currentURL, $currentPage +1, '&gt;&gt;');
		}
		if ($currentPage != $numPages) {
			$nav .= createPageLink($currentURL, $numPages, 'Last');
		}
	}
	return $nav;
}

/**
 * Creates an individual page link for the paging navigation.
 * 
 * @param $link The link for the page, defaulted to the current document.
 * @param $pageNum The page number of the link.
 * @param $displayText The text of the link.
 * @param $currentPage Indicates if the link is to the current page.
 * @param $qs An optional query string to be appended to the link.
 */
function createPageLink($link='', $pageNum, $displayText, $currentPage=false, $qs='') {
	global $modx;
	//$orderBy= !empty($_GET['orderby'])? '&orderby=' . $_GET['orderby']: '';
	//$orderDir= !empty($_GET['orderdir'])? '&orderdir=' . $_GET['orderdir']: '';
	if (!empty($qs)) $qs= "?$qs";
	$link= empty($link)? $modx->makeUrl($modx->documentIdentifier, $modx->documentObject['alias'], $qs . "page=$pageNum"): prepareLink($link) . "page=$pageNum";
	$nav .= '<li'.($currentPage? ' class="currentPage"': '').'><a'.($currentPage? ' class="currentPage"': '').' href="'.$link.'">'.$displayText.'</a></li>'."\n";
	return $nav;
}

/**
 * Function to prepare a link generated in the table cell/link actions.
 * 
 * @param $value Indicates the INPUT form element type attribute.
 */
function prepareLink($link) {
	if (strstr($link, '?')) {
		$end= '&';
	} else {
		$end= '?';
	}
	return $link.$end;
}


?>

